//
//  ZZCVisitorView.swift
//  weibo
//
//  Created by zhangzuchao on 17/3/27.
//  Copyright © 2017年 zhangZuChao. All rights reserved.
//

import UIKit
import SnapKit

//定义访客视图的代理协议  方便主控制器回调
protocol ZZCVisitorViewDelegate: NSObjectProtocol{

func loginOperation()
}


class ZZCVisitorView: UIView {

   //懒加载控件
    //旋转视图
    private lazy var cycleImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //蒙版
    private lazy var maskIconImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //主页图片
        private lazy var homeImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //文字信息
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.darkGray
        label.textAlignment = .center
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        return label
    }()
    
    //按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action:  #selector(loginAction), for: .touchUpInside)
        button.setTitle("注册", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        //背景图片拉伸
        UIImage().resizableImage(withCapInsets: UIEdgeInsetsMake(1, 1, 1, 1), resizingMode: UIImageResizingMode.stretch)
        
        //设置背景图片
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        //取消高亮
        button.adjustsImageWhenHighlighted = false
        return button
        }()
    //登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action:  #selector(loginAction), for: .touchUpInside)
        button.setTitle("登录", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        //背景图片拉伸
         UIImage(named:"common_button_white_disable")?.resizableImage(withCapInsets: UIEdgeInsetsMake(1, 1, 1, 1), resizingMode: UIImageResizingMode.stretch)
        
        //设置背景图片
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        //取消高亮
        button.adjustsImageWhenHighlighted = false
        return button
    }()

    //设置属性
    weak var delegate:ZZCVisitorViewDelegate?
    //定义闭包
    var loginClosure: (()->())?
    
    //重写加载方法
    override init(frame: CGRect){
    super.init(frame: frame)
        setupUI()
    
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //  添加控件设置约束
    private func setupUI(){
        backgroundColor = UIColor(white: 237.0/255, alpha: 1)
        addSubview(cycleImageView)
        addSubview(maskIconImageView)
        addSubview(homeImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //用snp自动约束
        cycleImageView.snp.makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        maskIconImageView.snp.makeConstraints { (make) in
            
            make.center.equalTo(cycleImageView)
        }
        homeImageView.snp.makeConstraints { (make) in
            
            make.center.equalTo(maskIconImageView)
        }
        messageLabel.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(homeImageView)
            make.top.equalTo(cycleImageView.snp.bottom)
            make.width.equalTo(224)
        }
        registerButton.snp.makeConstraints { (make) in
            
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp.bottom).offset(10)
            make.size.equalTo(CGSize(width: 80, height: 35))

        }
        loginButton.snp.makeConstraints { (make) in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
    }
    
    //旋转动画
    private func startAnimation() {
        //  创建动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //  目的地
        animation.toValue = 2 * Double.pi
        //  时长
        animation.duration = 20
        //  动画的执行次数
        animation.repeatCount = MAXFLOAT
        //  动画失去焦点,动画会释放, 切换其他页面会停止动画
        animation.isRemovedOnCompletion = false
        //  执行动画
        cycleImageView.layer.add(animation, forKey: nil)

    }

    //修改访客视图的信息,一次封装,4个页面都可以使用
    func updateVisitorInfo(imageName:String?,message: String?){
        if let imageName = imageName,let msg = message {
            //非首页
            homeImageView.image = UIImage(named:imageName)
            messageLabel.text = msg
            cycleImageView.isHidden = true
        } else{
        //首页
            startAnimation()
        }
    }
    
    
    @objc private func loginAction(){
        print("登录")
        
        //执行闭包
        loginClosure?()

    }
 
}
